/***
 * $HeadURL: http://gstestbbs.googlecode.com/svn/trunk/sfmforum/src/main/java/com/sfmforum/core/log/util/SfmForumLoggerResourcesLoader.java $
 * 
 **/
package com.sfmforum.core.log.util;

import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sfmforum.core.util.config.SfmForumResourcesLoader;

/***
 * load properties from commonlog_xx_xx.xml or commonlog_xx_xx.properties for
 * SfmForumLogger
 * 
 * @author $Author: nathanliaogs@gmail.com $
 * @version $Revision: 22 $
 * @modified $Date: 2012-03-25 16:14:59 +0000 (Sun, 25 Mar 2012) $
 * 
 **/
public class SfmForumLoggerResourcesLoader {
	private static Log log = LogFactory
			.getLog(SfmForumLoggerResourcesLoader.class);

	private final static String COMMON_LOG = "commonlog";

	private final static String EXT = ".properties";

	private final static String XML_EXT = ".xml";

	private static Properties properties;

	/**
	 * @param local
	 * @return
	 */
	public static Properties loadproperties(Locale local) {
		if (local == null)
			local = Locale.getDefault();
		properties = new Properties();
		try {
			// ---- load properties from commonlog_xx_xx.xml ------
			InputStream is = SfmForumResourcesLoader
					.loadResourcesToInputStream(COMMON_LOG + "_"
							+ local.toString() + XML_EXT);

			if (is == null) {
				is = SfmForumResourcesLoader
						.loadResourcesToInputStream(COMMON_LOG + "_"
								+ Locale.getDefault().toString() + XML_EXT);
			}

			if (is == null) {
				is = SfmForumResourcesLoader
						.loadResourcesToInputStream(COMMON_LOG + XML_EXT);
			}

			// ---- load properties from commonlog_xx_xx.properties ------
			if (is == null) {
				is = SfmForumResourcesLoader
						.loadResourcesToInputStream(COMMON_LOG + "_"
								+ local.toString() + EXT);
			}

			if (is == null) {
				is = SfmForumResourcesLoader
						.loadResourcesToInputStream(COMMON_LOG + "_"
								+ Locale.getDefault().toString() + EXT);
			}
			if (is == null) {
				is = SfmForumResourcesLoader
						.loadResourcesToInputStream(COMMON_LOG + EXT);
			}

			if (is == null) {
				log.warn("can not find log resource files, use normal log mode!");
			} else {
				properties.load(is);
			}

		} catch (Exception e) {
			log.warn("can not find log resource files, use normal log mode!");
		}
		return properties;
	}

	public static Properties getLoggerProperties(Locale local) {
		if (properties == null) {
			loadproperties(local);
		}
		return properties;
	}

	public static Properties getLoggerProperties() {
		if (properties == null) {
			loadproperties(null);
		}
		return properties;
	}
}
